package com.akuhome.resource.config;

import com.akuhome.resource.job.MonthReportJob;
import com.akuhome.resource.job.YearReportJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail monthReportJobDetail() {
        return JobBuilder.newJob(MonthReportJob.class)
                .withIdentity("MONTH_REPORT")
                .storeDurably()
                .build();
    }

    @Bean
    public JobDetail yearReportJobDetail(){
        return JobBuilder.newJob(YearReportJob.class)
                .withIdentity("YEAR_REPORT")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger monthTrigger() {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 0 1 * ?")
                .withMisfireHandlingInstructionFireAndProceed();

        return TriggerBuilder.newTrigger()
                .forJob(monthReportJobDetail())
                .withIdentity("MONTH_TRIGGER")
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public Trigger yearTrigger(){
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 2 0 1 1 ? *")
                .withMisfireHandlingInstructionFireAndProceed();

        return TriggerBuilder.newTrigger()
                .forJob(yearReportJobDetail())
                .withIdentity("YEAR_TRIGGER")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
